<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="一级品类" prop="oneGenreId">
        <el-select
          v-model="queryParams.oneGenreId"
          placeholder="请选择一级品类"
          clearable
          @change="handleOneProductGenreChange"
        >
          <el-option
            v-for="oneGenre in oneGenrees"
            :key="oneGenre.genreId"
            :label="oneGenre.name"
            :value="oneGenre.genreId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级品类" prop="twoGenreId">
        <el-select
          v-model="queryParams.twoGenreId"
          placeholder="请选择二级品类"
          :disabled="twoGenrees.length === 0"
        >
          <el-option
            v-for="twoGenre in twoGenrees"
            :key="twoGenre.genreId"
            :label="twoGenre.name"
            :value="twoGenre.genreId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:product:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:product:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:product:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:product:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
         <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:product:add']"
          >审核通过</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:product:add']"
          >审核不通过</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="60"></el-table-column>
      <el-table-column label="商品ID" align="center" prop="productId" />
      <el-table-column label="一级品类" align="center" prop="oneGenreName" />
      <el-table-column label="二级品类" align="center" prop="twoGenreName" />
      <el-table-column label="商品编号" align="center" prop="productNo" />
      <el-table-column label="商品名称" align="center" prop="name" />

      <el-table-column label="审核状态" align="center" prop="approveStatus">
        <template #default="scope">
          <dict-tag
            :options="product_approve_status"
            :value="scope.row.approveStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="销售状态" align="center" prop="onlineStatus">
        <template #default="scope">
          <dict-tag
            :options="product_onlinestatus"
            :value="scope.row.onlineStatus"
          />
        </template>
      </el-table-column>

      <el-table-column label="显示顺序" align="center" prop="floor" />
      <el-table-column label="市场价(元)" align="center" prop="listPrice" >
           <template  #default="scope">
            {{(Math.round(scope.row.listPrice)/100).toFixed(2)}}
            </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            >详细</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:product:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:product:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      v-model:visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属企业" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入所属企业" />
        </el-form-item>
        <el-form-item label="供应商ID" prop="enterId">
          <el-input v-model="form.enterId" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="一级类型ID" prop="oneGenreId">
          <el-input v-model="form.oneGenreId" placeholder="请输入一级类型ID" />
        </el-form-item>
        <el-form-item label="二级类型ID" prop="twoGenreId">
          <el-input v-model="form.twoGenreId" placeholder="请输入二级类型ID" />
        </el-form-item>
        <el-form-item label="三级类型ID" prop="threeGenreId">
          <el-input
            v-model="form.threeGenreId"
            placeholder="请输入三级类型ID"
          />
        </el-form-item>
        <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌ID" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名字" prop="NAME">
          <el-input
            v-model="form.NAME"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="显示顺序" prop="FLOOR">
          <el-input v-model="form.FLOOR" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="售卖热点" prop="saleWord">
          <el-input
            v-model="form.saleWord"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="结算企业ID" prop="settleCompanyId">
          <el-input
            v-model="form.settleCompanyId"
            placeholder="请输入结算企业ID"
          />
        </el-form-item>
        <el-form-item label="开始销售时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始销售时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束销售时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束销售时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品描述" prop="MEMO">
          <el-input
            v-model="form.MEMO"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="包装规格" prop="PACKAG">
          <el-input
            v-model="form.PACKAG"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="产品总售卖" prop="totalSold">
          <el-input v-model="form.totalSold" placeholder="请输入产品总售卖" />
        </el-form-item>
        <el-form-item label="市场价" prop="listPrice">
          <el-input v-model="form.listPrice" placeholder="请输入市场价" />
        </el-form-item>
        <el-form-item label="销售价" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售价" />
        </el-form-item>
        <el-form-item label="结算价格" prop="settlePrice">
          <el-input v-model="form.settlePrice" placeholder="请输入结算价格" />
        </el-form-item>
        <el-form-item label="帮卖低价" prop="bagnmaiPrice">
          <el-input v-model="form.bagnmaiPrice" placeholder="请输入帮卖低价" />
        </el-form-item>
        <el-form-item label="价格开始范围" prop="startPrice">
          <el-input
            v-model="form.startPrice"
            placeholder="请输入价格开始范围"
          />
        </el-form-item>
        <el-form-item label="价格结束范围" prop="endPrice">
          <el-input v-model="form.endPrice" placeholder="请输入价格结束范围" />
        </el-form-item>
        <el-form-item label="限制可买数量" prop="buyNumber">
          <el-input v-model="form.buyNumber" placeholder="请输入限制可买数量" />
        </el-form-item>
        <el-form-item label="商品库存" prop="productNumber">
          <el-input v-model="form.productNumber" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="首付比例" prop="firstRatio">
          <el-input v-model="form.firstRatio" placeholder="请输入首付比例" />
        </el-form-item>
        <el-form-item label="最晚支付时间" prop="waitTime">
          <el-input v-model="form.waitTime" placeholder="请输入最晚支付时间" />
        </el-form-item>
        <el-form-item label="是否分期" prop="FENQI">
          <el-input v-model="form.FENQI" placeholder="请输入是否分期" />
        </el-form-item>
        <el-form-item label="是否预售" prop="BOOKING">
          <el-input v-model="form.BOOKING" placeholder="请输入是否预售" />
        </el-form-item>
        <el-form-item label="产品图片" prop="imageOne">
          <el-input v-model="form.imageOne" placeholder="请输入产品图片" />
        </el-form-item>
        <el-form-item label="${comment}" prop="imageTwo">
          <el-input v-model="form.imageTwo" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="imageThree">
          <el-input v-model="form.imageThree" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="imageFour">
          <el-input v-model="form.imageFour" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="imageFive">
          <el-input v-model="form.imageFive" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="产品总分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入产品总分" />
        </el-form-item>
        <el-form-item label="产品平均分" prop="averageScore">
          <el-input
            v-model="form.averageScore"
            placeholder="请输入产品平均分"
          />
        </el-form-item>
        <el-form-item label="半染色ID" prop="banId">
          <el-input v-model="form.banId" placeholder="请输入半染色ID" />
        </el-form-item>
        <el-form-item label="全染色ID" prop="quanId">
          <el-input v-model="form.quanId" placeholder="请输入全染色ID" />
        </el-form-item>
        <el-form-item label="焕享变色ID" prop="bianseId">
          <el-input v-model="form.bianseId" placeholder="请输入焕享变色ID" />
        </el-form-item>
        <el-form-item label="焕享颜色(多个)" prop="bianseColor">
          <el-input
            v-model="form.bianseColor"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="创建者" prop="createEmployeeId">
          <el-input
            v-model="form.createEmployeeId"
            placeholder="请输入创建者"
          />
        </el-form-item>
        <el-form-item label="更新者" prop="updateEmployeeId">
          <el-input
            v-model="form.updateEmployeeId"
            placeholder="请输入更新者"
          />
        </el-form-item>
        <el-form-item label="推荐时间" prop="commentTime">
          <el-date-picker
            clearable
            v-model="form.commentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择推荐时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上线时间" prop="onlineTime">
          <el-date-picker
            clearable
            v-model="form.onlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上线时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPageByParam,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/product/product.js";

import {
loadOneProductGenreList,
loadTwoProductGenreList,
} from "@/api/product/genre.js";
import { getCurrentInstance } from "vue";
export default {
  setup() {
    // 获取当前组件实例
    const instance = getCurrentInstance();
    const { product_approve_status } = instance.proxy.useDict(
      "product_approve_status"
    );
    const { product_onlinestatus } = instance.proxy.useDict(
      "product_onlinestatus"
    );
    
    return {
      product_approve_status,
      product_onlinestatus,
    };
  },
  name: "UnverifiledProduct",
  data() {
    return {
      oneGenrees:[],
      twoGenrees:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        step: "UNVERIFIED",
        companyId: null,
        enterId: null,
        oneGenreId: null,
        twoGenreId: null,
        threeGenreId: null,
        brandId: null,
        productNo: null,
        productType: null,
        NAME: null,
        STATUS: null,
        FLOOR: null,
        approveStatus: null,
        onlineStatus: null,
        commendStatus: null,
        saleType: null,
        saleWord: null,
        settleCompanyId: null,
        startTime: null,
        endTime: null,
        MEMO: null,
        PACKAG: null,
        totalSold: null,
        listPrice: null,
        salePrice: null,
        settlePrice: null,
        bagnmaiPrice: null,
        startPrice: null,
        endPrice: null,
        buyNumber: null,
        productNumber: null,
        firstRatio: null,
        waitTime: null,
        FENQI: null,
        BOOKING: null,
        imageOne: null,
        imageTwo: null,
        imageThree: null,
        imageFour: null,
        imageFive: null,
        totalScore: null,
        averageScore: null,
        banId: null,
        quanId: null,
        bianseId: null,
        bianseColor: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null,
        commentTime: null,
        onlineTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.loadOneProductGenre();
  },
  methods: {

    // 加载一级数据
    loadOneProductGenre() {
      loadOneProductGenreList().then(response => {
        this.oneGenrees = response
      });
    },
    // 加载二级数据
    loadTwoProductGenre(parentid) {
      loadTwoProductGenreList(parentid).then(response => {
        this.twoGenrees = response
      });
    },
         // 省份选择变化时触发
    handleOneProductGenreChange(value) {
     
      if(value === null || value ===''){
          this.twoGenrees = [];
          this.queryParams.twoGenreId = null;
      }else{
 
      this.queryParams.oneGenreId = value;   // 省份变化时清空已选择的城市
      this.loadTwoProductGenre(value);
      }
    },
    // 城市选择变化时触发
    handleTwoProductGenreChange(value) {
      this.form.area = '';  // 城市变化时清空已选择的区县
      this.loadDistricts(value);    // 加载对应的区县数据
   },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.step = "UNVERIFIED";
      queryPageByParam(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        companyId: null,
        enterId: null,
        oneGenreId: null,
        twoGenreId: null,
        threeGenreId: null,
        brandId: null,
        productNo: null,
        productType: null,
        NAME: null,
        STATUS: null,
        FLOOR: null,
        approveStatus: null,
        onlineStatus: null,
        commendStatus: null,
        saleType: null,
        saleWord: null,
        settleCompanyId: null,
        startTime: null,
        endTime: null,
        MEMO: null,
        PACKAG: null,
        totalSold: null,
        listPrice: null,
        salePrice: null,
        settlePrice: null,
        bagnmaiPrice: null,
        startPrice: null,
        endPrice: null,
        buyNumber: null,
        productNumber: null,
        firstRatio: null,
        waitTime: null,
        FENQI: null,
        BOOKING: null,
        imageOne: null,
        imageTwo: null,
        imageThree: null,
        imageFour: null,
        imageFive: null,
        totalScore: null,
        averageScore: null,
        banId: null,
        quanId: null,
        bianseId: null,
        bianseColor: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null,
        commentTime: null,
        onlineTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.productId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除编号为"' + productIds + '"的数据项？'
        )
        .then(function () {
          return delProduct(productIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "yanjing/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
